#ifndef HTTP_CONNECTION_H_
#define HTTP_CONNECTION_H_

#include "./../http_protocol/http_protocol_parser.h"
#include "./../socket_module/socket_connection.h"

#include <cstdint>
#include <functional>
#include <memory>
class Loop;
class SocketConnection;
class HttpConnection
{
public:
    using OnConnectFn    = std::function<void(HttpConnection *, int)>;
    using OnMessageFn    = std::function<void(HttpConnection *, HttpResponse *)>;
    using OnDisConnectFn = std::function<void(HttpConnection *)>;

public:
    explicit HttpConnection(Loop * loop);
    void InitCallbackFn(OnConnectFn && connect, OnMessageFn && message_fn, OnDisConnectFn && dis_connect_fn);
    int  SendRequest(HttpRequest & request) const;
    int  InitHttpConnect(const std::string & host, uint16_t port, int enable_tls);
    void Close();

private:
    void OnConnection(std::shared_ptr<SocketConnection>, int result);
    void OnMessage(std::shared_ptr<SocketConnection>, int result, SocketConnection::Message &);
    void OnError(std::shared_ptr<SocketConnection>, int result);

private:
    Loop *                            m_loop;
    std::shared_ptr<SocketConnection> m_connection;
    OnConnectFn                       m_on_connect_fn;
    OnMessageFn                       m_on_msg_fn;
    OnDisConnectFn                    m_on_disconn_fn;

private:
    HttpResponseProtocolParser m_parser;
};

#endif
